from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from app import bn, upload, deepseek, kline, problem, users, backtest  # 导入拆分的路由模块
from ws import bnsocket, order
from pathlib import Path
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(lifespan=order.lifespan)

# 允许跨域，方便前端调试
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 定义图片存储路径
UPLOAD_DIR = Path("db")
# 创建目录（如果不存在）
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
# 设置模板目录
templates = Jinja2Templates(directory="./")
# 挂载静态文件目录 (如 CSS、JS)
app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/")  # 访问首页
def dist_index(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

# # 注册路由
app.include_router(upload.router, prefix="/api/upload", tags=["Upload"])
app.include_router(deepseek.router, prefix="/api/deepseek", tags=["DeepSeek"])
app.include_router(bn.router, prefix="/api/binance", tags=["Binance"])
app.include_router(kline.router, prefix="/api/kline", tags=["kline"])
app.include_router(problem.router, prefix="/api/problem", tags=["problem"])
app.include_router(users.router, prefix="/api/users", tags=["users"])
app.include_router(backtest.router, prefix="/api/backtest", tags=["backtest"])
# ws
app.include_router(bnsocket.app, prefix="/ws/bn", tags=["bnsocket"])

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8888, reload=True)